﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class CreateEditUserControl : UserControl
    {
        Users ActiveUser= Program.GetActiveUser;
        Users SelectedUser = new Users();
        bool edit = false;

        public CreateEditUserControl()
        {
            InitializeComponent();

        }

        public CreateEditUserControl(DataGridViewRow parametars)
        {
            InitializeComponent();

            edit = true;

            SelectedUser.SetID(parametars.Cells[0].Value.ToString());
            SelectedUser.GetUserFromDB();
            ImeText.Text = SelectedUser.GetName();
            PrezimeText.Text = SelectedUser.GetSurname();
            KorisnickoImeText.Text = SelectedUser.GetUsername();
            LozinkaText.Text = SelectedUser.GetPassword();
            LevelText.Text = (SelectedUser.GetLevel()).ToString();
            EditUsersCheck.Checked = SelectedUser.EditUseres;
            EditProductsCheck.Checked = SelectedUser.EditProducts;
            ViewProductsChek.Checked = SelectedUser.ViewProducts;
            ViewWarehouseCheck.Checked = SelectedUser.ViewWarehouse;
            EditProjetsCheck.Checked = SelectedUser.EditProjects;
            ViewProjectsCheck.Checked = SelectedUser.ViewProjects;
        }

        private void btnCancelUser_Click(object sender, EventArgs e)
        {
            this.Hide();
            Program.GetMainForm.ShowControl(ControlsEnum.USERS_CONTROL);
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            
            SelectedUser.SetName(ImeText.Text);
            SelectedUser.SetSurname(PrezimeText.Text);
            SelectedUser.SetUsername(KorisnickoImeText.Text);
            SelectedUser.SetPassword(LozinkaText.Text);
            SelectedUser.SetLevel(Convert.ToInt16(LevelText.Text));
            SelectedUser.EditUseres = EditUsersCheck.Checked;
            SelectedUser.EditProducts = EditProductsCheck.Checked;
            SelectedUser.ViewProducts = ViewProductsChek.Checked;
            SelectedUser.ViewWarehouse = ViewWarehouseCheck.Checked;
            SelectedUser.EditProjects = EditProjetsCheck.Checked;
            SelectedUser.ViewProjects = EditProjetsCheck.Checked;
            SelectedUser.Finance = EditFinanceCheck.Checked;

            if (edit)
            {
                SelectedUser.UpdateUserDB();
            }
            else
            SelectedUser.AddUserInDB();

            Program.GetMainForm.ShowControl(ControlsEnum.USERS_CONTROL);
            this.Dispose();
        }






        private void btnDelete_Click(object sender, EventArgs e)
        {
            SelectedUser.DeleteUserFromDB();
            Program.GetMainForm.ShowControl(ControlsEnum.USERS_CONTROL);
            this.Dispose();
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }
    }
}
